﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsTestimonialTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Testimonial Business Object
    /// </summary>

    public class Testimonial : BOBase
    {


        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Testimonial() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the Testimonial.
        /// </summary>
        public int TestimonialID
        {
            get { return _testimonialID; }
            set
            {
                if (_testimonialID != value)
                {
                    if (OnPropertyChanging("TestimonialID", _testimonialID, value))
                    {
                        int oldValue = _testimonialID;
                        _testimonialID = value;

                        OnPropertyChanged("TestimonialID", oldValue, value);
                    }
                }
            }
        }  private int _testimonialID = -1;

        /// <summary>
        /// TestimonialContent.
        /// </summary>
        public string TestimonialContent
        {
            get { return _testimonialcontent; }
            set
            {
                if (_testimonialcontent != value)
                {
                    if (OnPropertyChanging("TestimonialContent", _testimonialcontent, value))
                    {
                        string oldValue = _testimonialcontent;
                        _testimonialcontent = value;
                        OnPropertyChanged("TestimonialContent", oldValue, value);
                    }
                }
            }
        }  private string _testimonialcontent = null;


        /// <summary>
        /// SendersName.
        /// </summary>
        public string SendersName
        {
            get { return _sendersname; }
            set
            {
                if (_sendersname != value)
                {
                    if (OnPropertyChanging("SendersName", _sendersname, value))
                    {
                        string oldValue = _sendersname;
                        _sendersname = value;
                        OnPropertyChanged("SendersName", oldValue, value);
                    }
                }
            }
        }  private string _sendersname = null;

        /// <summary>
        /// SendersImage.
        /// </summary>
        public string SendersImage
        {
            get { return _sendersimage; }
            set
            {
                if (_sendersimage != value)
                {
                    if (OnPropertyChanging("SendersImage", _sendersimage, value))
                    {
                        string oldValue = _sendersimage;
                        _sendersimage = value;
                        OnPropertyChanged("SendersImage", oldValue, value);
                    }
                }
            }
        }  private string _sendersimage = null;


        /// <summary>
        /// Date/time at which the Testimonial was created.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;

        /// <summary>
        /// Date/time at which the Testimonial was updated.
        /// </summary>
        public DateTime? UpdatedDate
        {
            get { return _updatedDate; }
            set
            {
                if (_updatedDate != value)
                {
                    if (OnPropertyChanging("UpdatedDate", _updatedDate, value))
                    {
                        DateTime? oldValue = _updatedDate;
                        _updatedDate = value;
                        OnPropertyChanged("UpdatedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _updatedDate = null;


        /// <summary>
        /// True iff the Admin has made this Testimonial Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty Testimonial object.
        /// </summary>
        public static Testimonial CreateNew()
        {
            Testimonial obj = new Testimonial();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.AddedDate = DateTime.Now;
            obj.UpdatedDate = DateTime.Now;
            obj.IsActive = true;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static Testimonial CreateNew(TestimonialCollection collection)
        {
            Testimonial obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (Testimonial testimonial in collection)
            {
                if (testimonial.TestimonialID < minVal)
                    minVal = testimonial.TestimonialID;
            }
            obj.TestimonialID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllTestimonial

        /// <summary>
        /// Returns a collection of all Testimonial.
        /// </summary>
        public static TestimonialCollection GetAllTestimonial()
        {
            TestimonialTableAdapter adapter = new TestimonialTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsTestimonial.TestimonialDataTable tbl = adapter.GetData(null);

            TestimonialCollection list = new TestimonialCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsTestimonial.TestimonialRow row in tbl.Rows)
                {
                    Testimonial testimonial = new Testimonial();
                    RelationalToObject(testimonial, row);
                    list.Add(testimonial);
                }
            }

            return (list);
        }

        #endregion

        #region GetTestimonialByID

        /// <summary>
        /// Returns an Testimonial object for the specified 'TestimonialID' or null if no Testimonial with the specified parameter(s) exists.
        /// </summary>
        public static Testimonial GetTestimonialByID(int Testimonialid)
        {
            TestimonialTableAdapter adapter = new TestimonialTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsTestimonial.TestimonialDataTable tbl = adapter.GetData(Testimonialid);

            if (tbl.Rows.Count == 1)
            {
                Testimonial testimonial = new Testimonial();
                RelationalToObject(testimonial, tbl.Rows[0]);
                return (testimonial);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'Testimonial' returned more than one record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified Testimonial.
        /// </summary>
        public static void Update(Testimonial obj)
        {
            bool updateTestimonial = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateTestimonial = (obj.ObjState != ObjectState.Unchanged);

                if (!updateTestimonial)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsTestimonial.TestimonialDataTable tbl = null;
            if (updateTestimonial)
            {
                TestimonialTableAdapter adapter = new TestimonialTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsTestimonial.TestimonialDataTable();
                dsTestimonial.TestimonialRow row = tbl.NewTestimonialRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (TestimonialID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("TestimonialID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((TestimonialContent == null) || (TestimonialContent.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("TestimonialContent", "TestimonialContent is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("Testimonial: TestimonialID={0}, TestimonialContent={1}, {2}", TestimonialID, TestimonialContent, SendersName));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more Testimonial objects.
    /// </summary>

    public class TestimonialCollection : List<Testimonial>
    {
        #region Types

        public class UserComparer : IComparer<Testimonial>
        {
            public int Compare(Testimonial t1, Testimonial t2)
            {
                return (String.Compare(t1.TestimonialContent, t2.TestimonialContent));
            }
        }

        #endregion

        #region Public Methods

        #region FindByTestimonialID

        public Testimonial FindByTestimonialID(int TestimonialID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].TestimonialID == TestimonialID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindTestimonialByQuestion

        /// <summary>
        /// Case insensitve compare.
        /// </summary>
        public Testimonial FindTestimonialByContent(string testimonialcontent)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].TestimonialContent == testimonialcontent)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
